﻿class Main extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            list: [],
            no: '',
            id: 0,
            counts: [],
            start: 1,
            pagesize: 15,
            keyword: "",

            type: 0,
            state: 'ON',
            typehtml: [],
            statehtml: [],
            merchantName: '',
            list_select_shop: '',
            chosefile: !1,
            shoplist: [],

        };
    }












    export() {

        let { } = this.state

        获取小票列表({ export: 1 }, res => {
            if (res.issuccess == 1) {

                let str = '申请时间,用户id,积分,审核人id,店名,审核员工号,审核时间\n'
                //`${topname}\n${housename}\n产品类型,高,宽,深度,单价,数量,总价,单位,立面说明,门板材质/柜体材质\n`
                const jsonData = []
                let total = 0;
                let totalcount = 0;

                // <td className="tconter">{(Number(x.singleprice)* (((Number(x.height)* Number(x.width))/1000000).toFixed(2))).toFixed(2)}</td>
                res.list.forEach(x => {
                    str += `${formatDate(new Date(x.updateDate))},${x.userid},${x.price},${x.checkuserid},${x.shopname},${x.shopnum},${formatDate(new Date(x.checkdate))}\n`
                })

                let uri = 'data:text/csv;charset=utf-8,\ufeff' + encodeURIComponent(str);
                let link = document.createElement("a");
                link.href = uri;
                link.download = formatDate(new Date()) + "小票列表.csv";
                document.body.appendChild(link);
                link.click();
                document.body.removeChild(link);
            }
        })

    }




    getdata() {
        let { start, pagesize, keyword, size, price, cname, sshword,list_select_shop } = this.state,shopid=0

      

        获取小票列表({ shopid: list_select_shop, start: start == 0 ? 0 : start - 1, pagesize }, res => {
            let { list, total } = res;
            // let cnamehtml = []
            // cnamehtml.push(<option value={''}>请选择</option>)

            if (start == 1) {
                let pagecount = Math.ceil(total / pagesize);
                this.setpager(pagecount, start);
                this.setState({ start })
            }
            this.setState({ list, total })
        })

    }



    componentDidMount() {

        获取全部商户列表(res=>{
            if(res.issuccess==1){
                this.setState({shoplist:res.list})
            }
        })
        this.getdata()


        $('.modal1').hide()
        // testdingcan(res=>{
        // })
        this.setState({ showloading: '' })
        // this.freshlist()

    }




    setpager(pagecount, start) {
        let counts = [];
        for (let i = 0; i < pagecount; i++) {
            counts.push(start + i)
        }
        this.setState({ counts, pagestart: 0 })
    }
    clickpage(e) {
        let { pagesize } = this.state
        this.setState({ pagestart: e, start: e * pagesize + 1 }, () => {
            this.getdata()
        })
        console.log(e, 'eee');

        // if(start==e){
        //     start+=pagesize;
        // }else{
        //     start =start+ e*pagesize
        // }

        // this.setState({ pagestart: e, start }, () => {
        //    this.getdata()
        // })
    }
    setpage(e) {
        let { pagesize, start } = this.state
        pagesize = parseInt(e)
        start = 1
        this.setState({ pagesize, start }, () => {
            // 
            this.getdata()
        })
    }
    inputchange(prop, e) {
        this.setState({ [prop]: e.target.value })
    }
    inputchange1(prop, id, e) {
        let that = this;
        if (prop == "state") {
            that.setState({ showloading: 'ON' })
            Shopsetstate(id, res => {
                if (res.issuccess == 1) {
                    alert("设置成功")
                    that.freshlist()
                } else {
                    that.setState({ showloading: '' })
                    alert(res.msg)
                }
            })
        } else if (prop == "isrecommend") {
            that.setState({ showloading: 'ON' })
            Shopsetrecommend(id, res => {
                if (res.issuccess == 1) {
                    alert("设置成功")
                    that.freshlist()
                } else {
                    that.setState({ showloading: '' })
                    alert(res.msg)
                }
            })
        }

    }
    search() {
        this.setState({ start: 1 }, () => {
            this.getdata()
        })
    }
    closemodal() {
        $('.modal1').hide()
    }

    reset() {
        this.setState({ state: 'ON', no: '' }, () => {
            this.freshlist()
        })
    }
    selectchange(prop, e) {
        this.setState({ [prop]: e.target.value }, () => {
            this.search()
        })
    }
    listselectfhchange(e) {
        this.setState({ list_select_shop: e.target.value }, () => {
            this.getdata()
        })
    }

    filechange(e) {
        var that = this;
        let reader = new FileReader();
        reader.onload = function (e) {
            const workbook = XLSX.read(e.target.result); // 从原始数据获取工作簿

            var first_sheet_name = workbook.SheetNames[0]; // 获得第一个工作表名称

            const worksheet = workbook.Sheets[first_sheet_name]; // 获取工作表

            let json = XLSX.utils.sheet_to_json(worksheet);
            console.log(worksheet, 'worksheet'); // 读取并打印A1端元格数据
            console.log(json, 'json'); // 读取并打印A1端元格数据

            if (json.length > 0) {
                let list = []
                json.forEach(x => {
                    list.push({
                        name: x['姓名']
                        , idcard: x['身份证号码']
                        , workplace: x['工作单位']
                        , worktype: x['人员类别']
                        , gender: x['性别']
                        , phone: x['手机号码']

                    })
                    //身份证号码	姓名	性别	手机号码	工作单位	人员类别
                })
                let jsonstr = JSON.stringify(list)

                
                // test1(jsonstr, res => {
                //     if (res.issuccess) {
                //         that.getdata()
                //     }
                // })
                console.log(jsonstr, 'jsonstr');
                // that.setState({list})
            }

        };
        reader.readAsArrayBuffer($('#file1')[0].files[0]);

    }

    del(id) {

        var a = confirm('确定要删除吗?'), that = this
        if (a) {
            删除审核记录(id, res => {
                if (res.success == 1) {
                    that.getdata()
                }
            })
        }
    }
    pre(e) {
        console.log(e, 'preview');
        this.setState({ showpreview: 'ON', src: e })
    }

    tod(id) {
        window.location.href = './applydetail.html?id=' + id
    }

    render() {
        let { list, size, price, sizehtml, sshword, keyword, pricehtml, cname, cnamehtml, merchantName, list_select_shop, shoplist } = this.state;

        return (
            <div className="breadLinebox">
                <div class="alertarea" id='alsucc' >
                </div>


                <Preview hidden={this.state.showpreview != 'ON'} src={this.state.src} />
                <p className="merchantName">{merchantName}</p>
                <div className='panel-default content_wrap'>
                    <div className="breadLine ">

                        <div className="bbtn">
                            {/* <p className="button l" onClick={this.export.bind(this)} >导出</p> */}


                            <p className="button l" onClick={this.export.bind(this)} >全部导出</p>

                            <div className="content select" >
                                <div className="p1">选择商户</div>
                                <select value={list_select_shop} onChange={this.listselectfhchange.bind(this)}>
                                    <option value={''} >请选择</option>
                                    {
                                        shoplist.map((x, i) => {
                                            return (
                                                <option key={i} value={x.id}>{x.name}</option>
                                            )
                                        })
                                    }
                                </select>
                            </div>

                        </div>

                        <input type="file" id='file1' onChange={this.filechange.bind(this)} style={{ display: "none" }} />

                    </div>


                    <div className="contentBox">

                        <table className="tab">
                            <thead>
                                <tr>
                                    <th className="tconter">申请时间</th>
                                    <th className="tconter">用户id</th>
                                    <th className="tconter">积分</th>
                                    <th className="tconter">审核人id</th>
                                    <th className="tconter">店名</th>
                                    <th className="tconter">审核员工号</th>
                                    <th className="tconter">审核时间</th>
                                    <th className="tconter">操作</th>
                                    {/* <th>是否新人专享</th>
                                    <th>是否会员专享</th> */}
                                </tr>
                            </thead>
                            <tbody>
                                {
                                    list.map((x, i) => {
                                        return (
                                            <tr key={i}>
                                                <td style={{ width: '120px' }}>{formatDate(new Date(x.updateDate))}</td>
                                                <td className="tconter">{x.userid}</td>
                                                <td className="tconter">{x.price}</td>
                                                <td className="tconter">{x.checkuserid}</td>
                                                <td className="tconter">{x.shopname}</td>
                                                <td className="tconter">{x.shopnum}</td>
                                                <td className="tconter">{formatDate(new Date(x.checkdate))}</td>
                                                <td className="tconter ps">

                                                    <p style={{ 'text-decoration': 'underline', 'cursor': "pointer" }}
                                                        onClick={this.pre.bind(this, x.tsrc)}>查看图片</p>

                                                    {/* <p style={{'color':'red','text-decoration':'underline','cursor':"pointer"}}
                             onClick={this.del.bind(this,x.id)}>删除</p> */}

                                                    {/* <p style={{'text-decoration':'underline','cursor':"pointer"}}
                             onClick={this.del.bind(this,x.id)}>同意</p>
                             <p style={{'text-decoration':'underline','cursor':"pointer"}}
                             onClick={this.del.bind(this,x.id)}>驳回</p>
                       */}
                                                </td>


                                            </tr>
                                        )
                                    })
                                }
                            </tbody>
                        </table>
                        {this.state.wulist}
                        <div className="pagerbox">
                            <p>共{this.state.total}条</p>
                            <Pager counts={this.state.counts} click={this.clickpage.bind(this)} change={this.setpage.bind(this)} index={this.state.pagestart} />
                        </div>
                    </div>

                </div>
            </div>


        )


    }
}

ReactDOM.render(
    <div>
        <TopLine />
        <div id='body'>
            <LeftMenu code='ticket' height='ticket' />
            <Main />
        </div>
    </div>
    // <Barrage/>
    , $('.container')[0]
)